package com.easepal7506a.project.ui.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.easepal7506a.project.R;
import com.easepal7506a.project.interfaces.IProMasaChoose;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.CloseProfessionalObserver;
import com.easepal7506a.project.observer.TimeObserver;
import com.easepal7506a.project.ui.iview.IProfessionalMassageView7506;
import com.easepal7506a.project.ui.presenter.ProfessionalMassagePresenter7506;
import com.easepal7506a.project.utils.ActivityUtils;
import com.easepal7506a.project.view.VerticalViewPager.VerticalAdapter;
import com.easepal7506a.project.view.VerticalViewPager.VerticalViewPager;
import com.easepal7506a.project.widget.LayoutShfs_7506;
import com.easepal7506a.project.widget.LayoutGzsy_7506;
import com.easepal7506a.project.widget.LayoutZtms_7506;
import com.easepal7506a.project.widget.LayoutRqzs_7506;
import com.easepal7506a.project.widget.LayoutQjsd_7506;
import com.ogawa.base.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 专业按摩
 */
public class ProfessionalMassageActivity7506 extends BaseActivity implements View.OnClickListener, IProMasaChoose, IProfessionalMassageView7506,
        CloseProfessionalObserver.onCloseProLis ,TimeObserver.onTimeUpdataLis {
    private LayoutZtms_7506 mPageOne;
    private LayoutQjsd_7506 mPageTwo;
    private LayoutRqzs_7506 mPageThree;
    private LayoutGzsy_7506 mPageFour;
    private LayoutShfs_7506 mPageFive;
    private VerticalAdapter mAdapter;
    private VerticalViewPager mVerticalViewPager;
    private List<View> mVerticalViews;
    private ImageView mIvPointOne;
    private ImageView mIvPointTwo;
    private ImageView mIvPointThree;
    private ImageView mIvPointFour;
    private ImageView mIvPointFive;
    private TextView mTvNameOne;
    private TextView mTvNameTwo;
    private TextView mTvNameThree;
    private TextView mTvNameFour;
    private TextView mTvNameFive;
    private List<ImageView> mPoints = new ArrayList<>();
    private List<TextView> mClassifys = new ArrayList<>();
    private ProfessionalMassagePresenter7506 mPresenter;
    private String mCurName;
    private String mCurCode;


    @Override
    public void initTitleBar() {
        super.initTitleBar();
        titleBar.setTitleNameGone();
    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        Log.e("is7506a","ProfessionalMassageActivity7506");
        Log.e("ProfessionalMass", "ProfessionalMassageActivity7506" + "===" + mCurName);
        mVerticalViews = new ArrayList<>();
        mCurCode = DataManager.getInst().getAuto();
        mPageOne = (LayoutZtms_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_ztms_7506, null);
        mPageTwo = (LayoutQjsd_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_qjsd_7506, null);
        mPageThree = (LayoutRqzs_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_rqzs_7506, null);
        mPageFour = (LayoutGzsy_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_gzsy_7506, null);
        mPageFive = (LayoutShfs_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_shfs_7506, null);
        mTvNameOne = findViewById(R.id.tv_name1);
        mTvNameTwo = findViewById(R.id.tv_name2);
        mTvNameThree = findViewById(R.id.tv_name3);
        mTvNameFour = findViewById(R.id.tv_name4);
        mTvNameFive = findViewById(R.id.tv_name5);
        mTvNameOne.setOnClickListener(this);
        mTvNameTwo.setOnClickListener(this);
        mTvNameThree.setOnClickListener(this);
        mTvNameFour.setOnClickListener(this);
        mTvNameFive.setOnClickListener(this);
        mPageOne.setAction(this);
        mPageTwo.setAction(this);
        mPageThree.setAction(this);
        mPageFour.setAction(this);
        mPageFive.setAction(this);

        mIvPointOne = findViewById(R.id.pm_potin_one);
        mIvPointOne.setSelected(true);
        mTvNameOne.setSelected(true);
        mIvPointTwo = findViewById(R.id.pm_potin_two);
        mIvPointThree = findViewById(R.id.pm_potin_three);
        mIvPointFour = findViewById(R.id.pm_potin_four);
        mIvPointFive = findViewById(R.id.pm_potin_five);
        mPoints.clear();
        mPoints.add(mIvPointOne);
        mPoints.add(mIvPointTwo);
        mPoints.add(mIvPointThree);
        mPoints.add(mIvPointFour);
        mPoints.add(mIvPointFive);
        mClassifys.clear();
        mClassifys.add(mTvNameOne);
        mClassifys.add(mTvNameTwo);
        mClassifys.add(mTvNameThree);
        mClassifys.add(mTvNameFour);
        mClassifys.add(mTvNameFive);
        mVerticalViewPager = findViewById(R.id.verticalViewPager);
        mVerticalViews.add(mPageOne);
        mVerticalViews.add(mPageTwo);
        mVerticalViews.add(mPageThree);
        mVerticalViews.add(mPageFour);
        mVerticalViews.add(mPageFive);
        mVerticalViewPager.setOnPageChangeListener(new OnPageChangeListenerImpl());
        mAdapter = new VerticalAdapter(mVerticalViews);
        mVerticalViewPager.setAdapter(mAdapter);
        mPresenter = new ProfessionalMassagePresenter7506(this);
        mPresenter.initData();
        CloseProfessionalObserver.getInst().registerObserver(this);
        TimeObserver.getInst().registerObserver(this);
    }

    @Override
    public void chooseMassage(String name, String Code) {
        if (TextUtils.isEmpty(mCurName) || !mCurName.equals(name)) {
            sendCommond(Code);
        }else {
            ActivityUtils.startProMasDetailActivity(this, name);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestory();
        CloseProfessionalObserver.getInst().removeObserver(this);
        TimeObserver.getInst().removeObserver(this);
    }

    @Override
    public void noticeChange(int page, int position) {
        makaChoose(page, position);
    }

    @Override
    public void closeActivity() {
        CloseProfessionalObserver.getInst().noticeonClose();
    }

    @Override
    public void startAdvance() {
        if(CommonUtil.isActivityTop(ProfessionalMassageActivity7506.class,this)){
            ActivityUtils.startAdvancedMassageActivity(this);
            this.finish();
        }

    }

    @Override
    public void startDetail(String name) {
        if(CommonUtil.isActivityTop(ProfessionalMassageActivity7506.class,this)){
            ActivityUtils.startProMasDetailActivity(this,name);
        }
    }


    @Override
    public int setLayoutId() {
        return R.layout.activity_professional_massage_7506;
    }



    @Override
    public void getName(String name) {
        mCurName = name;
    }



    @Override
    public void onCloseActivity() {
        this.finish();

    }

    @Override
    public void onupdataTime(String min, String sec) {
        doUpDataTime(min,sec);
    }

    @Override
    public void timeFinish() {
        super.timeFinish();
        if(CommonUtil.isActivityTop(ProfessionalMassageActivity7506.class,this)){
            showFinishDialog(this,"0", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancleFinishDialog();
                    ActivityUtils.startHomeActivity(ProfessionalMassageActivity7506.this);
                    ProfessionalMassageActivity7506.this.finish();
                }
            });
        }
    }
    @Override
    public void isPause(boolean isPause) {
        if(isPause){
            pauseTime();
        }
        else {
            reStarTime();
        }
    }
    class OnPageChangeListenerImpl implements VerticalViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < mPoints.size(); i++) {
                if (position == i) {
                    mPoints.get(i).setSelected(true);
                    mClassifys.get(i).setSelected(true);
                } else {
                    mPoints.get(i).setSelected(false);
                    mClassifys.get(i).setSelected(false);
                }
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_name1) {
            mVerticalViewPager.setCurrentItem(0);

        } else if (i == R.id.tv_name2) {
            mVerticalViewPager.setCurrentItem(1);

        } else if (i == R.id.tv_name3) {
            mVerticalViewPager.setCurrentItem(2);

        } else if (i == R.id.tv_name4) {
            mVerticalViewPager.setCurrentItem(3);

        } else if (i == R.id.tv_name5) {
            mVerticalViewPager.setCurrentItem(4);

        }

    }


    private void makaChoose(int page, int position) {

        if (page == 0) {
            mPageOne.setSelect(position);
            mPageTwo.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(-1);
        } else if (page == 1) {
            mPageTwo.setSelect(position);
            mPageOne.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(-1);
        } else if (page == 2) {
            mPageTwo.setSelect(-1);
            mPageOne.setSelect(-1);
            mPageThree.setSelect(position);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(-1);

        } else if (page == 3) {
            mPageTwo.setSelect(-1);
            mPageOne.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(position);
            mPageFive.setSelect(-1);

        } else if (page == 4) {
            mPageTwo.setSelect(-1);
            mPageOne.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(position);

        } else {
            mPageOne.setSelect(-1);
            mPageTwo.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(-1);
        }
        mVerticalViewPager.setCurrentItem(page);

    }


}
